package com.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.food.configure.Result;
import com.food.entity.Food;
import com.food.entity.FoodFormula;
import com.food.mapper.FoodFormulaMapper;
import com.food.mapper.FoodMapper;
import com.food.service.FoodService;
import com.food.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2021-04-04
 */
@Service
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food> implements FoodService {

    @Resource
    FoodMapper foodMapper;

    @Resource
    FoodFormulaMapper foodFormulaMapper;

    @Override
    public Result<Food> updateFood(Food food) {
        Result<Food> result = new Result<>();
        try {
            if (StringUtils.isEmpty(food.getFoodId())) {
                //如果ID为空，代表添加
                food.setFoodId(IDGenerator.getUUID("FOOD"));
                if(food.getAmount() == null) food.setAmount(0);
                foodMapper.insert(food);

                if(food.getFoodFormulaList() != null && food.getFoodFormulaList().size() != 0){
                    food.getFoodFormulaList().stream().forEach(item->{
                        item.setFoodFormulaId(IDGenerator.getUUID("FOFO"));
                        item.setFoodId(food.getFoodId());
                        foodFormulaMapper.insert(item);
                    });
                }
            } else {
                //如果ID不为空，代表修改
                foodMapper.updateById(food);

                if(food.getFoodFormulaList() != null && food.getFoodFormulaList().size() != 0){
                    QueryWrapper<FoodFormula> wrapper = new QueryWrapper<>();
                    wrapper.lambda().eq(FoodFormula::getFoodId,food.getFoodId());
                    foodFormulaMapper.delete(wrapper);
                    food.getFoodFormulaList().stream().forEach(item->{
                        item.setFoodFormulaId(IDGenerator.getUUID("FOFO"));
                        item.setFoodId(food.getFoodId());
                        foodFormulaMapper.insert(item);
                    });
                }
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Food> delFood(Food food) {
        Result<Food> result = new Result<>();
        try {
            if (StringUtils.isEmpty(food.getFoodId())) {
                result.setMessage("foodId can't be empty");
                return result;
            }
            foodMapper.deleteById(food.getFoodId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Food> batchDelFood(Food food) {
        Result<Food> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(food.getIds())) {
                String[] ids = food.getIds().split(",");
                for (String id : ids) {
                    foodMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Food> queryFood(Food food) {
        Result<Food> result = new Result<>();
        try {
            QueryWrapper<Food> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            if(!StringUtils.isEmpty(food.getFoodName())){
                wrapper.lambda().like(Food::getFoodName,food.getFoodName());
            }

            List<Food> foods = foodMapper.selectList(wrapper);
            result.setReturnList(foods);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 查找菜品名字
     * @param food
     * @return
     */
    @Override
    public Result<Food> queryFoodByPage(Food food) {
        Result<Food> result = new Result<>();

        try {
            if (food.getCurrent() == null || food.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            QueryWrapper<Food> wrapper = new QueryWrapper<>();
            if(!StringUtils.isEmpty(food.getFoodName())){
                wrapper.lambda().like(Food::getFoodName,food.getFoodName());
            }

            //查询条件拼接，仅限单表查询可用
            IPage<Food> foods = foodMapper.selectPage(new Page<>(food.getCurrent(), food.getSize()), wrapper);
            //菜品配方
            foods.getRecords().stream().forEach(item->{
                QueryWrapper<FoodFormula> wrapper1 = new QueryWrapper<>();
                wrapper1.lambda().eq(FoodFormula::getFoodId, item.getFoodId());
                List<FoodFormula> foodFormulas = foodFormulaMapper.selectList(wrapper1);
                item.setFoodFormulaList(foodFormulas);
            });

            result.setReturnPage(foods);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Food> queryFoodById(Food food) {
        Result<Food> result = new Result<>();
        try {
            Food returnFood = foodMapper.selectById(food.getFoodId());
            result.setReturnObj(returnFood);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
